/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ace.component;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import ace.system.BitmapFont;
import ace.system.Utilities;

/**
 *
 * @author TimotiusNC
 */
public class ButtonPush extends Button {
	protected  ButtonPush() {
		//Initialize
		m_Push				= true;
		m_Alignment			= Graphics.LEFT | Graphics.TOP;
		m_PointerAlignment	= m_Alignment;
		m_IsPointer1		= true; //Default pakai pointer 1

		m_PointerX	= 0;
		m_OffsetY	= 0;
		m_Font		= BitmapFont.TITLE;
		
		//Create if pointer is null
		if (s_Pointer == null) {
			try {
				s_Pointer = Image.createImage(Utilities.INTERFACE_FOLDER + "/selection.png");
			} catch (IOException ex) {}
		}
		if (s_Pointer2 == null) {
			try {
				s_Pointer2 = Image.createImage(Utilities.INTERFACE_FOLDER + "/selection1.png");
			} catch (IOException ex) {}
		}
	}

	public ButtonPush(int id, String imgPath, int x, int y){
		//Default
		this();
		
		m_ID	= id;
		m_X		= x;
		m_Y		= y;

		setImage(imgPath);
	}

	public ButtonPush(String buttonTitle, int id, int x, int y){
		//Default
		this();
		
		m_ID	= id;
		m_X		= x;
		m_Y		= y;

		//Set image
		m_Img = BitmapFont.getFont(BitmapFont.TITLE).renderTransparentText(buttonTitle, 0x000000);
	}

	public ButtonPush(String buttonTitle, int font, int id, int x, int y){
		//Default
		this();

		m_ID	= id;
		m_X		= x;
		m_Y		= y;
		m_Font	= font;
		
		//Set image
		m_Img = BitmapFont.getFont(font).renderTransparentText(buttonTitle, 0x000000);
	}
	
	public int getWidth() {
		return m_Img.getWidth();
	}
	
	public int getHeight() {
		return m_Img.getHeight();
	}

	public void setText(String text){
		m_Img = BitmapFont.getFont(m_Font).renderTransparentText(text, 0x00000000);
	}
	
	public void setAlignment(int align) {
		m_Alignment = align;
	}

	public void setPointerAlignment(int align){
		m_PointerAlignment = align;
	}
	
	public void setPush(boolean push) {
		m_Push = push;
	}

	public void setPointerPosition(int x, int y){
		m_PointerX = x;
		m_OffsetY = y;
	}

	public void setIsPointer1(boolean isPointer1){
		m_IsPointer1 = isPointer1;
	}

	public final void setImage(String imgPath){
		try {
			m_Img = Image.createImage(imgPath);
		} catch (IOException ex) {}
	}
	
	public void draw(Graphics g) {
		//Draw based on the state
		boolean NotSelected = !m_Push || m_State == Button.STATE_NONE;

		if(m_IsPointer1)
			g.drawImage(m_Img, m_X, m_Y, m_Alignment);
		else
			g.drawImage(m_Img, m_X + (NotSelected ? 0 : s_Pointer2.getWidth() + 4), m_Y, m_Alignment);
		
		if (!NotSelected){
			if(m_PointerX == 0){
				if(m_IsPointer1)
					g.drawImage(s_Pointer, m_X, m_Y, m_PointerAlignment);
				else
					g.drawImage(s_Pointer2, m_X, m_Y, m_PointerAlignment);
			}
			else{
				if(m_IsPointer1)
					g.drawImage(s_Pointer, m_PointerX, m_Y + m_OffsetY, m_PointerAlignment);
				else
					g.drawImage(s_Pointer2, m_PointerX, m_Y + m_OffsetY, m_PointerAlignment);
			}
		}
	}

	//DISPLAY
	protected Image		m_Img;
	protected int		m_Alignment, m_PointerAlignment;
	protected boolean	m_Push;
	protected int		m_Font;
	
	protected static Image	s_Pointer = null;
	protected static Image	s_Pointer2 = null;
	protected boolean		m_IsPointer1;
	protected int			m_PointerX, m_OffsetY;
}
